package com.jm.web.builder;


import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.form.baseitem.YarncontItem;
import com.jm.web.tags.form.YarncontTag;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 * yarncont builer
 * 2011/9/29 下午1:01:10
 */
public class YarncontBuilder extends BaseBuilder{
	protected YarncontItem yarncont = null ;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		yarncont = new YarncontItem();
		yarncont.InitTag(tag);
		yarncont.setFromTag(true);//设置此控件是tag生成的
		yarncont.setEmptysupported(false);
		//如果相同的yarncont前面已将加载过，则不需要重复加载
		if(this.getBaseTag().getPage().autoDatacontainsKey(this.getBaseTag().GetExtValue("name","")))
			yarncont.setLoad(true);
		
		if(tag.GetExtValue("template", false)){
			((YarncontTag)tag).setName("yarncont_tp");
			((YarncontTag)tag).setWidth("400px");
		}
		
		yarncont.init();
	}
	
	@Override
	public void RenderEnd(){
		StringBuilderEx sb = new StringBuilderEx();
		
		if(!this.getBaseTag().getPage().autoDatacontainsKey(yarncont.getName())){
			try {
				this.getBaseTag().getPage().addAutoData(yarncont.getName());
				
				this.getOut().println("<script>");
				this.getOut().print("$P.autodata['");
				this.getOut().print(yarncont.getName());
				this.getOut().print("']=");
				if(StringHelper.IsNullOrEmpty(yarncont.getOtherdata())){
					this.getOut().print("{");
					this.getOut().print(yarncont.getData().toString());
					this.getOut().print("};");
				}else{
					this.getOut().print("$P.autodata['");
					this.getOut().print(yarncont.getOtherdata());
					this.getOut().print("'];");
				}
				this.getOut().println("</script>");
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		sb.Reset();
		
		//註冊combobox
		if(!yarncont.isTemplate()){
			sb.Append("\r\n $('#%1$s').yarncont({%2$s});",
				StringHelper.Format("%1$s",yarncont.getId()),
				yarncont.GetExtValue("config",""));
		}else{
			sb.Append("\r\n $('#%1$s').yarncont({validate:false,%2$s});",
					StringHelper.Format("%1$s",yarncont.getId()),
					yarncont.GetExtValue("config","a:''"));
		}
		
		if(yarncont.isTemplate()){
			sb.Append("var config = {};");
			sb.Append("config['title']='成份輸入';");
			sb.Append("config['autoOpen']=%1$s;", "false");
			sb.Append("config['resizable']=false;");
			sb.Append("config['withClose']=false;");
			sb.Append("config['modal']=true;");
			sb.Append("config['height']=100;");
			sb.Append("config['width']=410;");
			sb.Append("config['buttons']=[{text:'確定',click:function(){$P.outYarncont();}},{text:'取消',click:function(){$P.getdialog('yarncont_tp').close();}}];");
			sb.Append("$P.dialogs['yarncont_tp']=new $P.dialog($('div[id=%1$s]').dialog(config));","yarncont_tp_div");
		}
		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
		yarncont.onRender2(this.getOut());
	}
}
